//
//  FanKuiViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/8/23.
//

#import "FanKuiViewController.h"
#import "KFViewController.h"
@interface FanKuiViewController ()<TZImagePickerControllerDelegate>
@property (nonatomic , strong) FSTextView * fsTextView;
@property (nonatomic , strong) SQCustomButton * addButton;

@property (nonatomic , strong)SQCustomButton * rightBtn;
@property (nonatomic ) BOOL addImage;
@end

@implementation FanKuiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"问题反馈");
    WS(weakSelf);
    [self zx_setRightBtnWithText:SQNSLocalString(@"客服") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        KFViewController * vc = [[KFViewController alloc]init];
        [weakSelf SQ_pushViewControllerFull:vc];
    }];
}

- (void)SQ_UIViewForSelfView{
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"二级背景");
    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
  
    UILabel * label1 = [[UILabel alloc]init];
    label1.text = SQNSLocalString(@"文案描述");
    label1.textColor = UIColor.blackColor;
    label1.font = FONT_REGULAR(14);
    [self.view addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(20);
    }];
    UIView * bgView = [[UIView alloc]init];
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(20);
            make.top.equalTo(label1.mas_bottom).offset(10);
            make.width.mas_equalTo(SCREEN_WIDETH-40);
            make.height.mas_equalTo(200);
    }];
    [bgView addSubview:self.fsTextView];
    [self.fsTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(bgView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    UILabel * label = [[UILabel alloc]init];
    label.text = SQNSLocalString(@"图片描述");
    label.textColor = UIColor.blackColor;
    label.font = FONT_REGULAR(14);
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(20);
            make.top.equalTo(bgView.mas_bottom).offset(20);
    }];
    [self.view addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(100);
            make.left.equalTo(self.view).offset(20);
        make.top.equalTo(label.mas_bottom).offset(10);
    }];
    
  
    [self.view addSubview:self.rightBtn];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH-40);
        make.height.mas_equalTo(50);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-50);
    }];
    WS(weakSelf);
    [self.addButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_addImageButtonClick];
    }];
    [self.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_ButtonClick];
    }];
}
-(void)SQ_ButtonClick{
    if (!self.addImage) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请上传问题截图!")];
        return;
    }
    if (![self.fsTextView.formatText isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请填写问题描述!")];
        return;
    }
    WS(weakSelf);
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool SQ_Image:self.addButton.image successBlock:^(BaseModel * _Nonnull httpModel) {
        [weakSelf SQ_setUpName:httpModel.data];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}
-(void)SQ_setUpName:(NSString*)header{
    if (![header isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"头像上传错误")];
        return;
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:header forKey:@"img"];
    [dic setObject:self.fsTextView.formatText forKey:@"content"];
    [HttpTool post:@"feedback/save" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"提交成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self SQ_popViewVC];
        });
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}

-(void)SQ_addImageButtonClick{
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = 1;
    [[Manager SQ_getCurrentViewController] presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if (photos.count<=0) {
            return;
        }
        UIImage * image = photos.firstObject;
        weakSelf.addButton.image = image;
        weakSelf.addImage = YES;
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
 
 
-(FSTextView*)fsTextView{
    if (!_fsTextView) {
        _fsTextView = [FSTextView textView];
        _fsTextView.font = FONT_REGULAR(14);
        _fsTextView.textColor = UIColor.blackColor;
        _fsTextView.placeholder =SQNSLocalString(@"请告诉我您的困扰~");
        _fsTextView.placeholderColor = SQColor666666;
        _fsTextView.backgroundColor = UIColor.clearColor;
        _fsTextView.maxLength = 300;
       
    }
    return _fsTextView;
}
- (SQCustomButton *)addButton{
    if (!_addButton) {
        _addButton = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(100, 100) midmargin:0];
        _addButton.image = SQGetImage(@"tianjiatupian");
        _addButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _addButton.clipsToBounds = YES;
        _addButton.layer.cornerRadius = 10;
        _addButton.layer.masksToBounds = YES;
    }
    return _addButton;
}

- (SQCustomButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _rightBtn.text = SQNSLocalString(@"提交");
        _rightBtn.SQBackimageView.contentMode = UIViewContentModeScaleAspectFill;
        _rightBtn.SQBackimageView.image = SQGetImage(@"vip矩形");
        _rightBtn.textColor = UIColor.whiteColor;
        _rightBtn.font = FONT_REGULAR(16);
        _rightBtn.backgroundColor = SQColor3DA59D;
        _rightBtn.layer.cornerRadius = 24;
    }
    return _rightBtn;
}

@end
